import sys

from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QMainWindow, QStackedWidget, QApplication, QMessageBox
from PyQt5.QtCore import pyqtSignal


class Page1(QWidget):
    sing1 = pyqtSignal(int, str)

    def __init__(self):
        super().__init__()
        self.initUi()
        self.btn.clicked.connect(self.jump2)
        self.btn2.clicked.connect(self.alertMsg)

    def initUi(self):
        boxLayout = QVBoxLayout()
        self.btn = QPushButton("跳转页面2")
        boxLayout.addWidget(self.btn)
        self.btn2 = QPushButton("弹出框")
        boxLayout.addWidget(self.btn2)
        self.setLayout(boxLayout)

    def jump2(self):
        self.sing1.emit(1, 'sb')
        self.parentWidget().setCurrentIndex(1)

    def alertMsg(self):
        # QMessageBox.about(self, '关于框', "这是一个显示信息的框")
        choice = QMessageBox.warning(self, '错误框', '请做出你的选择',
                                      QMessageBox.Yes | QMessageBox.No)
        print("错误框的选择是:", "Yes" if choice == QMessageBox.Yes else "No")


class Page2(QWidget):
    def __init__(self):
        super().__init__()
        boxLayout = QVBoxLayout()
        btn = QPushButton("跳转页面1")
        btn.clicked.connect(self.jump1)
        boxLayout.addWidget(btn)
        self.setLayout(boxLayout)

    def jump1(self):
        self.parentWidget().setCurrentIndex(0)

    def printSingal1(self, aa, str1):
        print("页面1发出信号={},name={}".format(aa, str1))

    def printSingal2(self, aa, str1):
        print("页面1发出信号222={},name={}".format(aa, str1))


class MainPage(QMainWindow):
    def __init__(self):
        super().__init__()
        stackWeiget = QStackedWidget()
        page1 = Page1()
        page2 = Page2()
        page1.sing1.connect(page2.printSingal1)
        page1.sing1.connect(page2.printSingal2)
        stackWeiget.addWidget(page1)
        stackWeiget.addWidget(page2)
        self.setCentralWidget(stackWeiget)
        self.setFixedWidth(800)
        self.setFixedHeight(400)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainPage = MainPage()
    mainPage.show()
    sys.exit(app.exec_())
